﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using iPublic;

namespace HRecycle
{
    /// <summary>
    /// 回收站前台
    /// 修改记录：
    ///   20171212，zch
    /// </summary>
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        public iPublic.API.WoMen women = null;

        private void FrmMain_Load(object sender, EventArgs e)
        {   //初始化
            try
            {
                showInfo("★★ 程 序 启 动 ★★");
                foreach (ToolStripMenuItem itm in mnuMain.Items) { itm.Visible = false; }
                if (!iDefine.debugMode) FormBorderStyle = FormBorderStyle.None;
                WindowState = FormWindowState.Maximized;
                picLogo.Height = 916;
                pl_bottom2.Dock = DockStyle.Bottom;
                lbl_app.Top = pl_bottom2.Top - lbl_app.Height - 10;
                pic_app.Top = lbl_app.Top - pic_app.Height - 10;
                lbl_gongzhonghao.Top = lbl_app.Top;
                pic_gongzhonghao.Top = pic_app.Top;
                
                //读取参数
                iDefine.SysConfig.readConfig();

                //几个timer的间隔设置
                tm_xintiao.Interval = 1000 * 60 * 5;        //5分钟触发一次
                tm_guanggaoSync.Interval = 1000 * 60 * 30;  //30分钟刷新一次广告
                tm_guanggao.Interval = 1000 * 6 * 1;       //1分钟没动的，就开始显示广告
                //启用广告计时器
                tm_guanggao.Enabled = true;                 //广告显示计时器
                tm_guanggaoSync.Enabled = true;             //广告刷新计时器
                tm_xintiao.Enabled = true;                  //心跳包计时器
                //启动其他几个
                tm_runOnce.Enabled = true;                  //开机运行一次计时器
                tm_task.Enabled = true;                     //自定义其他任务计时器
            }
            catch (Exception x)
            {
                showInfo("FormLoad错误：" + x.Message);
            }
        }
        private void tm_runOnce_Tick(object sender, EventArgs e)
        {
            Timer tm = (sender as Timer);
            tm.Enabled = false;      //关闭自身，只运行一次
            try
            {
                //应用其他设置
                lbl_machineID.Text = iDefine.SysConfig.machineID;
                //串口
                if (iDefine.comHelper == null) iDefine.comHelper = new iComHelper();
                //我们
                if (women == null) women = new iPublic.API.WoMen() { showInfoProc = new iDefine.showInfoProc(showInfo) };
                //心跳一次
                tm_xintiao_Tick(tm_xintiao, e);
                //刷新一次广告
                tm_guanggaoSync_Tick(tm_guanggaoSync, e);
            }
            catch (Exception x)
            {
                showInfo("RunOnce错误：" + x.Message);
            }
        }

        private void btn_scan_Click(object sender, EventArgs e)
        {
            iDefine.ChuLiJieGuo result = new iDefine.ChuLiJieGuo(), jg = null;
            Comm.FrmShiBie frmShiBie = null;
            SysTools.FrmInputVIP frmInputVIP = null;
            Comm.FrmWanCheng frmWanCheng = null;
            DialogResult dlg = DialogResult.None;
            string sVipID = "";
            //停止广告计时器
            setGuanggao计时器(false);
            try
            {
                //清空所有项目
                iDefine.toufangList = new iDefine.ToufangItemList();
                iDefine.toufangList.Clear();

                do
                {
                    //识别物品id、是否可留
                    frmShiBie = new Comm.FrmShiBie();
                    dlg = frmShiBie.ShowDialog();
                    if (!frmShiBie.IsDisposed) frmShiBie.Dispose();
                    if (iDefine.toufangList == null || iDefine.toufangList.Count < 1)
                    {
                        iPublicX.showMessage("投放或者识别错误！", "错误");
                        goto lblPEnd;
                    }
                    //输入会员ID
                    frmInputVIP = new SysTools.FrmInputVIP();
                    SysTools.FrmInputVIP.sVipID = "";
                    dlg = frmInputVIP.ShowDialog();
                    if (!frmInputVIP.IsDisposed) frmInputVIP.Dispose();
                    sVipID = SysTools.FrmInputVIP.sVipID;
                }
                while (dlg == DialogResult.Retry);
                //判断类型处理
                if (dlg == DialogResult.Cancel)
                {
                    showInfo("☆Main：您投放了一堆东西，然后放弃不要积分了，高风亮节啊！☆");
                    goto lblPEnd;
                }

                //拿到了用户id和物品id，统一提交到服务器上
                if (women == null) women = new iPublic.API.WoMen();
                jg = women.积分绑定用户通过用户名为用户增加积分(sVipID);
                if (jg.error != 0) throw new Exception("绑定积分出错：" + jg.error.ToString() + "," + jg.message);
                iDefine.toufangList.A绑定用户增加积分 = jg.data as iDefine.json绑定用户增加积分;

                //全部处理完毕
                frmWanCheng = new Comm.FrmWanCheng();
                frmWanCheng.ShowDialog();
                if (!frmWanCheng.IsDisposed) frmWanCheng.Dispose();

                lblPEnd:
                result.error = 0;
            }
            catch (Exception x)
            {
                showInfo(x.Message);
                iPublicX.showMessage(x.Message);
            }
            //回复广告计时器
            setGuanggao计时器(true);
        }


        private void tm_task_Tick(object sender, EventArgs e)
        {   //任务设定，暂时用不到
            lbl_date.Text = DateTime.Now.ToString("yyyy/MM/dd");
            lbl_shifenmiao.Text = DateTime.Now.ToString("HH:mm");
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            setGuanggao计时器(false);          //广告计时器开关
            //登陆
            SysTools.FrmLogin frm = new SysTools.FrmLogin();
            DialogResult dr = frm.ShowDialog();
            if (!frm.IsDisposed) frm.Dispose();
            setGuanggao计时器(true);           //广告计时器开关
            if (dr != DialogResult.OK) return;
            //登陆后
            foreach (ToolStripMenuItem itm in mnuMain.Items) { itm.Visible = true; }
            FormBorderStyle = FormBorderStyle.Sizable;
        }

        public void showInfo(string sInfo, bool lTime = true)
        {
            string s = sInfo;
            lbl_info.Text = sInfo;
            if (lTime) s = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " " + sInfo;
            iLog.writeLogFile(sInfo);
        }

        private void mnu_log_main_Click(object sender, EventArgs e)
        {
            iDefine.SysConfig.fUserID = "";
            foreach (ToolStripMenuItem itm in mnuMain.Items) { itm.Visible = false; }
            FormBorderStyle = FormBorderStyle.None;
        }

        private void mnu_close_main_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认关闭系统吗？", "确认", MessageBoxButtons.YesNo) != DialogResult.Yes) return;
            iLog.writeLogFile("关闭系统" + iDefine.SysConfig.sUserID());
            Close();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {   //关闭，释放资源
                showInfo("■■ 程 序 退 出 ■■");
                if (iDefine.comHelper != null) iDefine.comHelper.closePort();
                if (women != null) women = null;
            }
            catch (Exception x)
            {
                showInfo(x.Message);
            }
        }

        private void mnu_sys_set_Click(object sender, EventArgs e)
        {
            setGuanggao计时器(false);          //广告计时器开关
            SysTools.FrmSysConfig frm = new SysTools.FrmSysConfig();
            frm.ShowDialog();
            if (!frm.IsDisposed) frm.Dispose();
            setGuanggao计时器(true);           //广告计时器开关
        }

        private void mnu_sys_hwDebug_Click(object sender, EventArgs e)
        {
            setGuanggao计时器(false);          //广告计时器开关
            SysTools.FrmDebugHW frm = new SysTools.FrmDebugHW();
            frm.ShowDialog();
            if (!frm.IsDisposed) frm.Dispose();
            setGuanggao计时器(true);           //广告计时器开关
        }

        private void mnu_work_openFront_Click(object sender, EventArgs e)
        {
            if (iDefine.comHelper == null) iDefine.comHelper = new iComHelper();
            iDefine.comHelper.开上前门锁();
        }

        private void mnu_work_openBack_Click(object sender, EventArgs e)
        {
            if (iDefine.comHelper == null) iDefine.comHelper = new iComHelper();
            iDefine.comHelper.开下前门锁();
        }

        private void mnu_help_about_Click(object sender, EventArgs e)
        {
            setGuanggao计时器(false);          //广告计时器开关
            SysTools.FrmAbout frm = new SysTools.FrmAbout();
            frm.ShowDialog();
            if (!frm.IsDisposed) frm.Dispose();
            setGuanggao计时器(true);           //广告计时器开关
        }

        private void tm_xintiao_Tick(object sender, EventArgs e)
        {   //心跳包，每隔5分钟向服务器上报一次自己状态
            Timer tm = sender as Timer;
            bool lEnable = tm.Enabled;
            tm.Enabled = false;             //关闭自身
            try
            {
                if (women == null) women = new iPublic.API.WoMen();
                iDefine.ChuLiJieGuo jg = women.向服务器提交状态();
                if (jg.error != 0) showInfo("心跳包发送出错：" + jg.error.ToString() + "," + jg.message);
                else showInfo("心跳包" + (tm.Interval / 1000).ToString() + "发送成功：" + jg.message);
            }
            catch (Exception x)
            {
                showInfo("心跳包发送出错：" + x.Message);
            }
            tm.Enabled = lEnable;
        }

        public void setGuanggao计时器(bool lEnable, bool l重置从0开始 = false)
        {
            if (l重置从0开始 && lEnable) tm_guanggao.Enabled = false;
            tm_guanggao.Enabled = lEnable;
        }
        Comm.FrmGuangGao frmGuangGao = null;
        public void showGuanggao()
        {
            setGuanggao计时器(false);          //广告计时器开关
            if (frmGuangGao == null)
            {
                frmGuangGao = new Comm.FrmGuangGao();
            }
            frmGuangGao.ShowDialog();
            frmGuangGao.TopMost = true;
            frmGuangGao.TopMost = false;
            //show完了之后不释放
        }
        private void tm_guanggao_Tick(object sender, EventArgs e)
        {   //广告显示
            if (!iDefine.SysConfig.guanggaoEnable) return;
            Timer tm = sender as Timer;
            bool lEnable = tm.Enabled;
            tm.Enabled = false;                     //关闭自身
            try
            {
                showGuanggao();
            }
            catch (Exception x)
            {   //出错之后，释放之
                if (frmGuangGao != null && !frmGuangGao.IsDisposed)
                {
                    frmGuangGao.Dispose();
                }
                frmGuangGao = null;
            }
            tm.Enabled = true;
        }

        private void tm_guanggaoSync_Tick(object sender, EventArgs e)
        {
            if (women == null) women = new iPublic.API.WoMen();
            women.刷新广告设置();
        }

        
        private void mnu_work_guanggao_Click(object sender, EventArgs e)
        {
            showGuanggao();
            setGuanggao计时器(true);
        }

        private void btn_openBack_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            setGuanggao计时器(false);
            SysTools.FrmOpenBack frm = new SysTools.FrmOpenBack();
            frm.ShowDialog();
            if (!frm.IsDisposed) frm.Dispose();
            setGuanggao计时器(true);
        }

    }

}
